using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseForum.Model;
using WebForum.App_Data;

namespace WebForum
{
    public partial class ForumManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Insert")
            {
                Forum forum = new Forum();
                forum.Title = ((TextBox)GridView1.FooterRow.FindControl("TextBoxAddTitle")).Text;
                forum.Desc = ((TextBox)GridView1.FooterRow.FindControl("TextBoxAddDesc")).Text;
                forum.Position = Common.ParseInt32(((TextBox)GridView1.FooterRow.FindControl("TextBoxAddPosition")).Text);
                if (ForumDataSource.Insert(forum))
                {
                    Common.Show("Insert forum sucess!", Request.RawUrl);
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            int fID = Convert.ToInt32(((Label)GridView1.Rows[index].FindControl("LabelId")).Text);
            if (ForumDataSource.Delete(fID))
            {
                Common.Show("Delete forum sucess!", Request.RawUrl);
            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int fID = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("LabelId")).Text);
            Forum forum = ForumDataSource.Get(fID);
            forum.Title = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBoxTitle")).Text;
            forum.Desc = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBoxDesc")).Text;
            forum.Position = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBoxPosition")).Text);
            if (ForumDataSource.Update(forum))
            {
                Common.Show("Update forum sucess!", Request.RawUrl);
            }
        }
    }
}
